package com.gpframework.module.order.service.impl;

import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.module.order.domain.GpProductDetails;
import com.gpframework.module.order.domain.vo.GpProductDetailsVo;
import com.gpframework.module.order.service.IGpProductDetailsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.order.mapper.GpProductMapper;
import com.gpframework.module.order.domain.GpProduct;
import com.gpframework.module.order.domain.vo.GpProductVo;
import com.gpframework.module.order.service.IGpProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
/**
 * @ClassName GpProduct
 * @Author gpframework
 * @Date 2023-06-16
 * @Version 1.0.0
 * @Description 产品Service业务层处理
 */
@Service
public class GpProductServiceImpl extends ServiceImpl<GpProductMapper, GpProduct> implements IGpProductService {

    @Autowired
    private IGpProductDetailsService gpProductDetailsService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-06-16
     * @Param [lqw]
     * @Return List<GpProductVo>
     */
    @Override
    public List<GpProductVo> listVo(QueryWrapper<GpProduct> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-06-16
     * @Param [id]
     * @Return GpProductVo
     */
    @Override
    public GpProductVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public GpProductVo[] productListByOrderId(Long orderId) {
        List<GpProductVo> gpProductVos = this.listVo((new QueryWrapper<GpProduct>().eq("order_id", orderId).eq("del_status", YesNoEnum.NO.getKey())));
        for (GpProductVo gpProductVo : gpProductVos) {
            List<GpProductDetailsVo> gpProductDetailsVos = gpProductDetailsService.listVo(new QueryWrapper<GpProductDetails>().eq("product_id", gpProductVo.getId()).eq("del_status", YesNoEnum.NO.getKey()));
            if (CollectionUtils.isNotEmpty(gpProductDetailsVos)) {
                gpProductVo.setDetails(gpProductDetailsVos.toArray(new GpProductDetailsVo[0]));
            }
        }
        if (CollectionUtils.isNotEmpty(gpProductVos)) {
            return gpProductVos.toArray(new GpProductVo[0]);
        }
        return new GpProductVo[0];
    }
}
